// pages/index/index.js
const dayjs = require('dayjs')
import CustomPage from '../../base/CustomPage'

const nowDate = dayjs().format('YYYY-MM-DD')
CustomPage({
    /**
     * 页面的初始数据
     */
    data: {
        endDate: nowDate,
        loading: false,
        formData: {
            title: '',
            startDay: nowDate,
            nickName: '',
            location: {},
            locationText: '',
            music: {
                pic: 'cloud://everyday-4gkd6una188bf952.6576-everyday-4gkd6una188bf952-1302485943/musics/pics/别错过.jpeg',
                url: 'cloud://everyday-4gkd6una188bf952.6576-everyday-4gkd6una188bf952-1302485943/musics/别错过.m4a'
            },
            musicText: '🎵 别错过 (DJ阿卓版)',
            wordsOfLove: ''
        },
        rules: require('./common/rules'),
        words_of_love_results: []
    },
    bindDateChange: function (e) {
        const {
            field
        } = e.currentTarget.dataset
        this.setData({
            [`formData.${field}`]: e.detail.value
        })
    },
    formInputChange(e) {
        const {
            field
        } = e.currentTarget.dataset
        this.setData({
            [`formData.${field}`]: e.detail.value
        })
    },
    onSubmit() {
        this.selectComponent('#form').validate((valid, errors) => {
            // console.log('valid', valid, errors)
            if (!valid) {
                const firstError = Object.keys(errors)
                if (firstError.length) {
                    this.setData({
                        error: errors[firstError[0]].message
                    })
                }
                return
            }
            this.setData({
                loading: true
            })
            wx.cloud.callFunction({
                name: 'remind',
                data: {
                    action: 'createData',
                    ...this.data.formData
                },
                success: res => {
                    // console.log('[云函数] [remind] user openid: ', res.result)
                    wx.hideLoading()
                    wx.navigateTo({
                        url: `/pages/index/success?id=${res.result._id}`,
                    })
                    this.setData({
                        loading: false,
                        id: res.result._id
                    })
                },
                fail: err => {
                    console.error('[云函数] [remind] 调用失败', err)
                    this.setData({
                        loading: false
                    })
                    wx.navigateTo({
                        url: '/pages/index/fail',
                    })
                }
            })
        })
    },
    getWordsOfLoves() {
        wx.cloud.callFunction({
            name: 'words_of_love',
            success: (res) => {
                const words_of_love_results = res.result || []
                const wordsOfLove = words_of_love_results[parseInt(Math.random() * words_of_love_results.length)] || {}
                // console.log(words_of_love_results, wordsOfLove)
                words_of_love_results.length && this.setData({
                    words_of_love_results,
                    ['formData.wordsOfLove']: wordsOfLove.content || ''
                })
            }
        })
    },
    musicbindtap() {
        wx.showToast({
          title: '音乐设置功能即将开放',
          icon: 'none'
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getWordsOfLoves()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    onShareTimeline() {

    },
})